﻿Imports System.Data.SqlClient

Public Class FormCategoryEditor

   
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim cn As New SqlConnection()
        cn.ConnectionString = "server=.;uid=sa;pwd=123;database=SaleDb;"
        cn.Open()

        Dim cmd As New SqlCommand()
        cmd.Connection = cn
        cmd.CommandText = "INSERT INTO Category VALUES(N'" & txtCategoryName.Text & "',N'" & txtTypeId.Text & "',1)"

        cmd.ExecuteNonQuery()
        Me.Close()

    End Sub

    

    Private Sub btnOK_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If txtCategoryId.Text = "" Then
            Dim cn As New SqlConnection()
            cn.ConnectionString = "server=.;uid=sa;pwd=123;database=SaleDb;"
            cn.Open()

            Dim cmd As New SqlCommand()
            cmd.Connection = cn
            cmd.CommandText = "INSERT INTO Category VALUES(N'" & txtCategoryName.Text & "',N'" & txtTypeId.Text & "',1)"
            cmd.ExecuteNonQuery()
        Else
            Dim cn As New SqlConnection()
            cn.ConnectionString = "server=.;uid=sa;pwd=123;database=SaleDb;"
            cn.Open()

            Dim cmd As New SqlCommand()
            cmd.Connection = cn
            cmd.CommandText = " UPDATE Category SET CategoryName=N'" & txtCategoryName.Text & "',TypeId=N'" & txtTypeId.Text & "' WHERE CategoryId =" & txtCategoryId.Text
            cmd.ExecuteNonQuery()
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK



    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel

    End Sub
End Class